home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / Content Creation Tool Plug-Ins / Maya / Sources / XExporter.h < prev   
Encoding:
C/C++ Source or Header  |  2004-09-27  |  10.6 KB  |  398 lines

  1. #ifndef __XEXPORTER_H__
  2. #define __XEXPORTER_H__
  3.  
  4. //required to compile under vc7
  5. //#define REQUIRE_IOSTREAM
  6.  
  7. #include "PreviewPipeline.h"
  8. #include "MayaInterface.h"
  9. #include "device.h"
  10. #include "viewer.h"
  11.  
  12.  
  13.  
  14. #include <vector>
  15. #include <map>
  16.  
  17. #include <time.h>
  18. #include <sys/timeb.h>
  19. #include <sys/types.h>
  20.  
  21. #include <maya/MGlobal.h>
  22. #include <maya/MStringArray.h>
  23. #include <maya/MPxFileTranslator.h>
  24. #include <maya/MFnDependencyNode.h>
  25. #include <maya/MDagPath.h>
  26. #include <maya/MDagPathArray.h>
  27. #include <maya/MStatus.h>
  28. #include <maya/MFnMesh.h>
  29. #include <maya/MObject.h>
  30. #include <maya/MFnDependencyNode.h>
  31. #include <maya/MPlug.h>
  32. #include <maya/MDagMessage.h>
  33. #include <maya/MNodeMessage.h>
  34. #include <maya/MArgList.h>
  35. #include <maya/MFloatArray.h>
  36. #include <maya/MFnSkinCluster.h>
  37. #include <maya/MFloatMatrix.h>
  38.  
  39. using namespace std;
  40. ///////////////////////////////////////////////////////////////////////////////////////
  41. // DEBUG_CONTROL
  42. ///////////////////////////////////////////////////////////////////////////////////////
  43. //#define MAYA_ATTRIBUTE_DEBUG
  44.  
  45. ///////////////////////////////////////////////////////////////////////////////////////
  46. // DECLARATIONS
  47. ///////////////////////////////////////////////////////////////////////////////////////
  48. class DCCTag;
  49. class DCCTagContainer;
  50. class CMayaPreviewPipeline;
  51. class CMayaPreviewPipelineDevice;
  52. class DirectXShader;
  53.  
  54. #define DX_UPDATE_FREQUENCY            (1000.0f/15.0f)
  55.  
  56.  
  57.  
  58.  
  59.  
  60. extern CRITICAL_SECTION DeviceAndViewerSection; 
  61. extern HANDLE DeviceCreatedEvent; 
  62.  
  63.  
  64. //typedef struct FrameUserData        *LPFRAMEUSERDATA;
  65. //LPFRAMEUSERDATA    FrameGetUserData(LPDXCCFRAME pFrame, LPDXCCMANAGER pManager);                    
  66. //LPFRAMEUSERDATA    FrameCreateUserData(LPDXCCRESOURCE pRes);                
  67. typedef struct    MeshUserData            *LPMESHUSERDATA;
  68. LPMESHUSERDATA    MeshGetUserData(LPDXCCMESH pMesh, LPDXCCMANAGER pManager);            
  69. LPMESHUSERDATA    MeshCreateUserData(LPDXCCRESOURCE pRes, UINT cVerticesMaya, UINT cVerticesD3D);
  70. void            MeshCleanUserData(    LPMESHUSERDATA dxMeshUserData,     UINT cVerticesMaya,    UINT cVerticesD3D)    ;
  71.  
  72. bool    SkinMeshGetOriginalMesh(MFnMesh& fnMesh);
  73. HRESULT CalculateMeshSize(MFnMesh& fnMesh, UINT& cTriangles, UINT& cVertices);
  74.  
  75.  
  76. bool    DAGIsVisible(MDagPath& dagPath, MStatus* status);
  77. bool    NodeIsVisible(MObject& node, MStatus* _status);
  78.  
  79. UINT    MayaGetFPS();
  80.  
  81. void ConvertLocalMatrix(D3DXMATRIX& ToD3D, const MMatrix& FromMaya);
  82. void ConvertLocalMatrix(D3DXMATRIX& ToD3D, const MFloatMatrix& FromMaya);
  83. void ConvertWorldMatrix(D3DXMATRIX& ToD3D, const MMatrix& FromMaya);
  84. void ConvertWorldMatrix(D3DXMATRIX& ToD3D, const MFloatMatrix& FromMaya);
  85.  
  86.  
  87.  
  88. ///////////////////////////////////////////////////////////////////////////////////////
  89. // EXTERNS
  90. ///////////////////////////////////////////////////////////////////////////////////////
  91. extern CMayaPreviewPipeline g_PreviewPipeline;
  92.  
  93. ///////////////////////////////////////////////////////////////////////////////////////
  94. // DEFINITONS
  95. ///////////////////////////////////////////////////////////////////////////////////////
  96.  
  97. struct DCCVertexInfo
  98. {
  99.     MPoint            position;
  100.     MVector            normal;
  101.     MColor            color;
  102.     UINT            cUVs;
  103.     MStringArray    UVNames;
  104.     MFloatArray        Ucoords;
  105.     MFloatArray        Vcoords;
  106.  
  107.     DCCVertexInfo()
  108.     {
  109.         cUVs=0;
  110.     }
  111. };
  112.  
  113. struct DCCMeshInfo
  114. {
  115.  
  116.     UINT             cD3DTriangles; 
  117.     UINT             cD3DVertices;
  118.  
  119.     DCCVertexInfo    VertexInfo;
  120.  
  121.     DCCMeshInfo()
  122.     {
  123.         cD3DTriangles=0; 
  124.         cD3DVertices=0;
  125.     }
  126. };
  127.  
  128.  
  129. struct DXCCTimeStamp
  130. {
  131.     _timeb            Coarse;
  132.     clock_t            Fine;
  133.  
  134.     DXCCTimeStamp();
  135.     void AcquireTime();
  136.     bool RoughlyEqual(DXCCTimeStamp &test);
  137. };
  138.  
  139.  
  140. class DCCTag
  141. {
  142.     friend DCCTagContainer;
  143.     friend CMayaPreviewPipeline;
  144. protected:
  145.     ~DCCTag();
  146.     DCCTag() {};
  147.     DCCTag(DCCTagContainer* Owner, MDagPath& path);
  148. public:
  149.  
  150.     void            SetPath(MDagPath& newPath);
  151.     MDagPath&        GetPath();
  152.  
  153.     bool            IsValid();
  154.  
  155.     bool            IsInitialized();
  156.     bool            SetInitialized(bool bInitializeNow);
  157.     bool            CanInitialize();
  158.  
  159.  
  160.  
  161.     //DCCTag*            Parent;
  162.     //vector<DCCTag*>    Dependants;//
  163.  
  164. #ifdef MAYA_ATTRIBUTE_DEBUG
  165.     MStringArray    AttributeChanged;
  166. #endif
  167.  
  168.     DXCCTimeStamp    TimeStamp;
  169.     MCallbackId        PathCallback;
  170.     IDXCCResource*    Resource;
  171.     IUnknown*        Interface;
  172.     
  173.     //bool            DirtyAnimationFlag;
  174.  
  175.  
  176.     MDagPath            Path;
  177.     DCCTagContainer*    pContainer;
  178.     bool                Dirty;
  179.     bool                Initialized;
  180.  
  181. };
  182.  
  183. class DCCTagContainer
  184. {
  185.     friend DCCTag;
  186. public:
  187.     DCCTagContainer() {};
  188.     DCCTagContainer(CMayaPreviewPipeline* owner) 
  189.     {
  190.         pPreview= owner;
  191.         GlobalArray.clear();
  192.         DirtyArray.clear();
  193.     };
  194.  
  195.     ~DCCTagContainer() 
  196.     {
  197.         EraseAll();
  198.     };
  199.  
  200.     bool        Add(MObject& objAllPathsTo );
  201.     bool        Add(MDagPath& path, UINT *pGlobalTagIndex, DCCTag** ppTag );
  202.  
  203.     UINT        Size();
  204.     bool        Get(UINT iGlobalTagIndex, DCCTag* &tag );
  205.     bool        Find(DCCTag* pFind, UINT *pGlobalTagIndex);
  206.     bool        Find(MDagPath& path, UINT *pGlobalTagIndex, DCCTag** ppTag );
  207.  
  208.     UINT        SizeFromDirtyList();
  209.     bool        GetFromDirtyList(UINT iGlobalTagIndex, DCCTag* &tag); 
  210.     bool        FindFromDirtyList(DCCTag* pFind, UINT *pDirtyListIndex);
  211.     bool        FindFromDirtyList(MDagPath& path, UINT *pGlobalTagIndex, DCCTag** ppTag );
  212.  
  213.     bool        Erase(UINT iGlobalTagIndex);
  214.     bool        Erase_SLOW(DCCTag* tag);
  215.     void        EraseAll();    
  216.     void        EraseInvalidTags();
  217.  
  218.     void        MarkAsDirty(UINT iGlobalListIndex);
  219.     void        MarkAsDirty(DCCTag* tag);
  220.     void        MarkAsClean(UINT iDirtyListIndex);
  221.     void        MarkAsClean_SLOW(DCCTag* tag);
  222.     bool        IsDirty(DCCTag* tag);
  223.  
  224.     DXCCTimeStamp    TimeStamp;
  225. protected:
  226.     CMayaPreviewPipeline* pPreview;
  227.     vector<DCCTag*> DirtyArray;
  228.     vector<DCCTag*> GlobalArray;
  229.  
  230. };
  231.  
  232. //class DCCObject
  233. //{
  234. //    DCCObject(MObject& obj);
  235. //    DCCTagContainer* GetTagContainer();
  236. //    D3DXMATRIX GetLocalMatrix();
  237. //}
  238.  
  239.  
  240. class CMayaPreviewPipeline : public CPipeline
  241. {
  242.     friend DCCTagContainer;
  243.     friend DirectXShader;
  244. public:
  245.                         CMayaPreviewPipeline();
  246.     virtual                ~CMayaPreviewPipeline(); 
  247.  
  248.     virtual HRESULT        Create();
  249.     virtual HRESULT        Destroy();
  250.  
  251.     static void            Callback_MayaExiting( void* clientData );
  252.     static void            Callback_SceneSave(void* clientData );
  253.     static void            Callback_ScenePreload(void* clientData );
  254.     static void            Callback_ScenePostload(void* clientData );
  255.     static void            Callback_SceneIdle( void* clientData );
  256.     static void            Callback_NodeAdded( MObject & node, void* clientData );
  257.     static void            Callback_NodeRemoved( MObject & node, void* clientData );
  258.     static void            Callback_NodeNameChange( MObject & node, void* clientData );
  259.     static void            Callback_NodeAttributeChanged( MNodeMessage::AttributeMessage msg, MPlug & plug, MPlug & otherPlug, void* clientData );
  260.     static void            Callback_NodeParentingChange( MDagMessage::DagMessage dagMsg, MDagPath &child, MDagPath &parent, void * clientData );
  261.     //static void        Callback_AnimationEdit( MObjectArray &editedCurves, void *clientData );//for realtime attempts at tracking animation.//not using this approach yet
  262.  
  263.     void                Scene_Register();
  264.     void                Scene_Deregister();
  265.     void                Scene_Update(bool bForceUpdate);
  266.     void                Scene_Update_Tags(bool bForceUpdate, DXCCTimeStamp& TimeStamp, MFn::Type type);
  267.     void                Scene_Update_Materials(bool bForceUpdate, DXCCTimeStamp& TimeStamp);
  268.     void                Scene_Update_Visibility(bool bForceUpdate, DXCCTimeStamp& TimeStamp);
  269.     void                Scene_Update_Animation();
  270.     HRESULT                Scene_Export(const char* file, const char* options, MPxFileTranslator::FileAccessMode mode);
  271.  
  272.     HRESULT             Synchronize_Node(MObject & node); //Multi-instance DCC sync
  273.     HRESULT             Synchronize_DagPath(MDagPath &path); //single instance DCC sync
  274.     HRESULT             Synchronize_Tag(DCCTag *pTag);//single instance pipeline sync
  275.     HRESULT             Synchronize_Frame(DCCTag *pTag);//typed sync
  276.     HRESULT             Synchronize_Mesh(DCCTag *pTag);//typed sync
  277.     HRESULT             Synchronize_PerspectiveCamera(); 
  278.     HRESULT             Synchronize_Material(DXCCHANDLE hDirtyShader); //Multi-instance DCC sync
  279.  
  280.     HRESULT             Tag_Add(MObject & node);
  281.     HRESULT             Tag_Remove(MObject & node);
  282.     HRESULT             Tag_Rename( MObject & node);
  283.     HRESULT             Tag_Dirty(MNodeMessage::AttributeMessage msg, MPlug & plug, MPlug & otherPlug);
  284.     HRESULT             Tag_Reparent(MDagMessage::DagMessage dagMsg, MDagPath &child, MDagPath &parent, bool bFixInstances);
  285.     
  286.     HRESULT             Frame_GatherAnimation(DCCTag* pTag)    ;
  287.  
  288.     HRESULT                Mesh_CreateOrRecycle(DCCTag *pTag, MFnMesh &fnMesh, DCCMeshInfo &MayaMeshInfo);
  289.     HRESULT             Mesh_GatherMeshInfo(MFnMesh& fnMesh, DCCMeshInfo& MayaMeshInfo);
  290.     HRESULT             Mesh_GatherVertexInfo(MFnMesh& fnMesh, DCCVertexInfo& MayaVertexInfo);
  291.     HRESULT                Mesh_GatherMaterials( MObjectArray& Shaders,  LPDXCCMESH pMesh);
  292.     HRESULT                Mesh_FindSkin(MFnMesh& InOutMesh, MFnSkinCluster& SkinCluster);
  293.     HRESULT                Mesh_GatherSkin(DCCTag *pTag, MFnSkinCluster& SkinCluster);
  294.  
  295.     HRESULT                Material_Save(MObject &node);
  296.     HRESULT                Material_Restore(MObject &node);
  297.     
  298.     D3DXMATRIX&         PerspectiveCamera_GetView();
  299.     D3DXMATRIX&         PerspectiveCamera_GetProjection();
  300.  
  301.     bool                UI_GetPreviewState();
  302.     bool                UI_GetSkinState();
  303.     void                UI_SetPreviewState(bool bPreview);
  304.  
  305.     DXCCHANDLE            Convert_DGNodeToDXCCResource(MObject matchNode);
  306.     MObject                Convert_DXCCResourceToDGNode(DXCCHANDLE matchHandle);
  307.  
  308. ////////////////////////VARIABLES/////////////////////
  309.     BOOL m_bResyncScene;
  310.     DCCTagContainer TagContainer;
  311.     vector<DXCCHANDLE> DirtyShaders;
  312.     DCCTag DxRoot;
  313.     MDagPath MayaRoot;
  314.  
  315.  
  316.     MCallbackId CallbackId_Exiting;
  317.     MCallbackId CallbackId_AfterOpen;
  318.     MIntArray    CallbackId_PreviewArray;
  319.     MIntArray    CallbackId_NodeArray;
  320.  
  321.     CEngine g_Engine;
  322.     CViewer g_Viewer;
  323.  
  324.     D3DXMATRIX PerspectiveCamera_View;
  325.     D3DXMATRIX PerspectiveCamera_Projection;
  326.  
  327. protected:
  328.  
  329.  
  330. };
  331.  
  332. struct VertexD3DToMayaType
  333. {
  334.     VertexD3DToMayaType()
  335.     {
  336.         MayaPolygon= UNUSED32;
  337.         MayaVertex= UNUSED32;
  338.         D3DNextPointRep= UNUSED32;
  339.     };
  340.     DWORD MayaPolygon;
  341.     DWORD MayaVertex;
  342.     DWORD D3DNextPointRep;
  343. }; 
  344.  
  345. struct VertexMayaToD3DType
  346. {
  347.     VertexMayaToD3DType()
  348.     {
  349.         D3DFirstPointRep= UNUSED32;
  350.         D3DLastPointRep= UNUSED32;
  351.     };
  352.     DWORD D3DFirstPointRep;
  353.     DWORD D3DLastPointRep;
  354. };     
  355.  
  356. struct MeshUserData
  357. {
  358.  
  359.     UINT         cMayaPolygons;
  360.     UINT         cMayaEdges;
  361.     UINT         cMayaFaceVertices;
  362.     UINT         cMayaUVs;
  363.     UINT         cMayaUVSets;
  364.     UINT         cMayaVertices;
  365.  
  366.     //LPDXCCFRAME parent;
  367.     _timeb        lastUpdate;
  368.  
  369.  
  370.  
  371.     MeshUserData() //:
  372.         //parent(NULL),
  373.         //D3DPointReps(NULL),
  374.         //pVertexD3DToMaya(NULL),
  375.         //pVertexMayaToD3D(NULL)
  376.         {};
  377.  
  378. };
  379.  
  380.  
  381.  
  382.  
  383.  
  384. class CDXCCSaveEffectDefaults : public IDXCCEnumEffectParameters
  385. {
  386. public:
  387.     HRESULT Set(LPDXCCMANAGER pResourceManager, /*MObject& attribParameters,*/ MObject& pxNode);
  388.  
  389.     STDMETHOD(EnumParameter)(THIS_  LPD3DXEFFECT pEffect, PathInfo& parameter);
  390.  
  391.     LPDXCCMANAGER pManager;
  392.     MObject oNode;
  393.     UINT iParameter;
  394. };
  395.  
  396.  
  397.  
  398. #endif //__XEXPORTER_H__